define([ 'jquery', 'jquery_route', 'arttemplateUtil' ,'common','config','pageJs','validform','wdatePicker','bootstrapselect','jquerycookie'], function($,
		jquery_route, arttemplateUtil,common,config,pageJs  ) {
	// 查看反馈
	
	var initRecordfeedbackData
	$.controller("recordfeedbackController", function($model, $tplHtml, $defobj,
			$modelobj, $paramobj, $this) {
		var param={};
		param.userid=$paramobj.id;
		$defobj.param=param;
		$model($defobj);
		initRecordfeedbackData = function(){
			common.loading();
			// 查询地区
			$defobj.data={};
			param.userid=$paramobj.id;
			$defobj.param=param;
			common.ajax_request2('get','/api/seller/v1/recordfeedbackByUserId',param,function($data,$code){
				if($code==200){
					$defobj.data=$data;
				}
				App.htmlSetData('#recordfeedbackBody .feedbackWrap','order/templates/recordfeedback_tpl.html',$defobj);
				
				if(typeof $defobj.data != 'undefined' && typeof $defobj.data.resultData != 'undefined'){
					pageJs.setPage($defobj.data.resultData.pageNo,$defobj.data.resultData.totalPage,$defobj.data.resultData.totalCount,callback);
				}

				App.init();
				layer.closeAll('loading'); //关闭加载层
			});
		}
		//分页回调
		var callback = function(page){
			param.pageNo=page
			initRecordfeedbackData();
		}
		initRecordfeedbackData();
	});
	
	//互动详情
	var initInteractiveData;
	$.controller("interactiveController", function($model, $tplHtml, $defobj,
			$modelobj, $paramobj, $this) {

		$model($defobj);
//		$("#usertype").empty();
//		$.each(config.sysconfig().sender, function(i, item) {
//			$("#usertype").append("<option value=\""+item.value+"\">"+item.name+"</option>");
//		});
//		$("#usertype").append("<option value=\""+$paramobj.id+"\">用户</option>");
		
		var param={};
		initInteractiveData = function($msgtype){
			common.loading();
			// 查询地区
			$defobj.data={};
			param.userid=$paramobj.id;
			if(typeof $msgtype != 'undefined'){
				param.msgtype=$msgtype;
			}else{
				param.msgtype=$('#msgtype li.active a').attr('tab_value');
			}
			if(param.msgtype==0){
				$("#message_mobile_text").show();
			}else{
				$("#message_mobile_text").hide();
			}
			param.usertype=$('#usertype').val();
			param.message_title=$('#message_title').val();
			param.message_mobile=$('#message_mobile').val();
			param.first_message_date=$('#first_message_date').val();
			param.last_message_date=$('#last_message_date').val();
			$defobj.param=param;
			common.ajax_request2('post','/api/seller/v1/queryfeedbackByUserId',param,function($data,$code){
				if($code==200){
					$defobj.data=$data;
					$('#interactive_list .username').html($data.mobile);
					$('#interactive_list .msgcount').html($data.serverfbCount);
				}
				
				$('#interactive_list .tab-content .tab-pane').empty();
				App.htmlSetData('#interactive_list #panel-'+param.msgtype,'order/templates/interactive_tpl.html',$defobj);

				if(typeof $defobj.data != 'undefined' && typeof $defobj.data.interactionData != 'undefined'){
					pageJs.setPage($defobj.data.interactionData.pageNo,$defobj.data.interactionData.totalPage,$defobj.data.interactionData.totalCount,callback);
				}
				layer.closeAll('loading'); //关闭加载层
			});
		}
		//分页回调
		var callback = function(page){
			param.pageNo=page
			initInteractiveData();
		}

		var _interactiveFrm=$('#interactiveBody #serchForm');
		// 搜索
		$('#interactiveBody #serchForm .serchBtn').off('click').on('click',function(){
			initInteractiveData();
		});
		// 重置
		$('#interactiveBody #serchForm .resetBtn').off('click').on('click',function(){
			if(_interactiveFrm.length>0){
				_interactiveFrm[0].reset();
			}
			param.pageNo=1;
			initInteractiveData();
		});
		//选中标签
		$('#interactiveBody #msgtype li').off('click').on('click',function(){
			initInteractiveData($(this).find('a').attr('tab_value'));
		});
		
		initInteractiveData();
	});
	
	
	var App={
			init:function(){
				//初始化绑定查看事件
				$('#recordfeedbackBody').off('click','.historyFeedback').on('click','.historyFeedback',function(){
					var param={};
					param.recordid=$(this).attr('data-id');
					param.endtime=$(this).attr('data-time');
					param.user_id=$('#recordfeedbackBody').attr('data-userid');
					common.ajax_request2('get','/api/seller/v1/queryFeedBack',param,function($data,$code){
						if($code==200){
							var _tmpobj={};
							_tmpobj.dataCheck=true;
							_tmpobj.dataAttr=' disabled';
							_tmpobj.dataList=$data.serverfb;
							_tmpobj.dataCount=$data.serverfbcount;
							App.htmlSetData('#historyFeedback .serverfb','order/templates/feedback_tpl.html',_tmpobj);
							_tmpobj.dataList=$data.productfb;
							_tmpobj.dataCount=$data.productfbcount;
							App.htmlSetData('#historyFeedback .productfb','order/templates/feedback_tpl.html',_tmpobj)
							$('#historyFeedback').modal('show');
						}
					});
				});
				

				//初始化绑定添加事件
				$('#recordfeedbackBody').off('click','.addFeedback').on('click','.addFeedback',function(){
					var param={};
					param.user_id=$('#recordfeedbackBody').attr('data-userid');
					if($('#addFeedback form').length>0){
						$('#addFeedback form')[0].reset();
					}
					common.ajax_request2('get','/api/seller/v1/editFeedBack',param,function($data,$code){
						if($code==200){
							var _tmpobj={};
							_tmpobj.dataList=$data.serverfb;
							_tmpobj.dataCount=$data.serverfbcount;
							App.htmlSetData('#addFeedback .serverfb','order/templates/feedback_tpl.html',_tmpobj);
							_tmpobj.dataList=$data.productfb;
							_tmpobj.dataCount=$data.productfbcount;
							App.htmlSetData('#addFeedback .productfb','order/templates/feedback_tpl.html',_tmpobj);
							$('#addFeedback form').attr('data-once','true');
							$('#addFeedback').modal('show');
						}
					});
				});


				//初始化绑定保存事件
				$('#addFeedback ').off('click','.btnsave').on('click','.btnsave',function(){
					if($('#addFeedback form input[type=checkbox]:checked').length>0){
						if($('#addFeedback form').attr('data-once') == 'true'){
							$('#addFeedback form').removeAttr('data-once');
							common.ajax_request2('post','/api/seller/v1/saveFeedBack',$('#addFeedback form').serializeArray(),function($data,$code){
								$('#addFeedback form').attr('data-once','true');
								if($code==200){
									$('#addFeedback').modal('hide');
									window.layer.msg("保存成功");
									initRecordfeedbackData();
								}
							});
						}else{
							window.layer.msg("重复提交");
						}
					}else{
						window.layer.msg("请勾选反馈项");
						$('#addFeedback form input[type=checkbox]:first').focus();
					}
				});
			},
			htmlSetData:function(out_id,html_url,dataobj){
				$(out_id).html(arttemplateUtil.template.compile($.setHTML(html_url))(dataobj));
			}
	};
});